<?php
/*
 * Created on 09.01.2008
 *
 * filename: menu.php5
 */

  class Controller_Menu extends Controller_Base {

		private $items_counter = 0;
		private $items = array();

		function index($args) {
			print "index";
		}

  	 	function load_json($args) {
			$uid = intval ($args[0]);
			
			$out = $this->menu->getHierarchicalMenu(array(uid => $uid));	

			$json = new Services_JSON();

			// printing out JSON
			echo $json->encode($out);
  	 	}

  	 	function full_load_json($args) {
			
			$sesid = ($_COOKIE['sesid']) ? ($_COOKIE['sesid']) : ($_GET['sesid']);

			$id = $this->menu->getIdByAlias('access');
			// for full menu need access to admin area
//			$this->checkSession($sesid,$id);
			$uid = intval ($args[0]);
			$out = $this->menu->getHierarchicalMenu(array(uid => $uid, show => 'all'));	

			$json = new Services_JSON();

			// printing out JSON
			echo $json->encode($out);
 		
  	 	}
  	 	
  	 	
  	 	function save_from_json() {
  	 		
//  	 		error_log("start save tree");

  	 		foreach ($_POST['sortable_list']['items'] as $key => $value) {
				$this->items[] = array(
					id 			=> $value['id'],
					parent		=> 1, 
					order 		=> $this->get_counter()
				); 
//				error_log($value['id']." -> 0 [".$this->get_counter()."]");
				$this->addItem($value);
  	 		}

			$this->addSystemPartitions();

			foreach ($this->items as $row) {
				error_log($row);
			}
  	 		
  	 		$this->menu->addNewRelations($this->items);
  	 		
//  	 		error_log("end save tree");
  	 		
  	 		$this->flash_counter();
  	 	}
  	 	
  	 	//private recursive method [for save from json]
  	 	private function addItem($parent) {

			if($parent['children']) {

				foreach ($parent['children'] as $k => $child) {
					$this->items[] = array(
						parent 		=> $parent['id'], 
						id		=> $child['id'], 
						order		=> $this->get_counter()
					);
//					error_log($parent['id']." -> ".$child['id'] ." [".$this->get_counter()."]");
					$this->addItem($child);
				}
			}
	  	 	
  	 	}
  	 	
  	 	//resets items counter
  	 	private function flash_counter() {
  	 		$this->items_counter = 0;
  	 	}

		//increase counter value and returns value
  	 	private function get_counter() {
  	 		return ++$this->items_counter;
  	 	}

		//private recursive method [for load json]
		function addNode ($item,$parent,&$flat_array) {

//			error_log("parent: $parent | $item->parent_id |  $item->id ");
			
			$new = array();
			
			if($item->parent_id == $parent) {
				$new[id] 	= $item->id;
				$new[info]	= array("\"$item->value\"");
				
				//	array_shift($flat_array);
				foreach($flat_array as $k => $v) {
					$children = $this->addNode($v,$new[id],$flat_array);
					if($children) $new[children][] = $children;
				}
				
			}
		
			return $new;
		}

		private function addSystemPartitions()	{
			
			foreach ($this->menu->getSystemItems() as $item) {
				
				$this->items[] = $item;
//error_log( $this->items_counter." ".var_dump($item));
//				$this->items[] = array(
//					id 		=> $item->id,
//					parent	=> $item->parent_id,
//					order	=> ++$this->items_counter
//				);
			} 

		}

  }
?>
